<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/admin/AdminAction.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Employee.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Person.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Post.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class EmployeeAddAction extends AdminAction {
	protected function execute(Response $response) {
		$person = null;
		if(isset($_POST['id_person']) && isset($_POST['id_post']) && isset($_POST['rate'])) {
			$employee = new Employee();
			// так как Employee подкласс класса Post, то идентификатор должности
			// записываем в Employee.identity
			$employee->setIdentity($_POST['id_post']);
			$person = new Person();
			$person->setIdentity($_POST['id_person']);
			$employee->setPerson($person);
			$employee->setRate($_POST['rate']);
			$serviceFactory = new ServiceFactory();
			$service = $serviceFactory->getService('EmployeeService');
			$employee = $service->save($employee);
		}
		if($person !== null) {
			$_SESSION['person-identity'] = $person->getIdentity();
			$response->setActionPath('/person/edit.html');
		} else {
			$response->setActionPath('/person/index.html');
		}
	}
}
?>